formRef<template>
  <div class="login relative">
    <div class="login_form flex-center">
      <el-form
        ref="formRef"
        style="max-width: 600px"
        :model="formData"
        status-icon
        size="large"
        :rules="rules"
        label-width="auto"
        class="demo-formData"
      >
        <div class="login_title">欢迎登录</div>
        <el-form-item label="" prop="username">
          <el-input v-model.number="formData.username" placeholder="请输入用户名：admin" />
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input v-model="formData.password" type="password" autocomplete="off" placeholder="请输入密码：rb123456" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" @click.prevent="submitForm(formRef)">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="bottom-info">
      <span>Copyright 2002-2022</span>
      <span>陕ICP备2021016258号-1</span>
      <span class="pointer" @click="goCompany"
        >技术支持：陕西北佳技术软件有限公司</span
      >
      <span>电话：029-81020945</span>
    </div>
  </div>
</template>
<script setup>
import { sm4 } from 'gm-crypt'
import { useUserStore } from "@/store"

const userStore = useUserStore()

const router = useRouter()
const route = useRoute()

const formData = reactive({
  username: '',
  password: ''
})
const formRef = ref()
const loading = ref(false) // 按钮 loading 状态
const validateName = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入用户名'))
  } else {
    callback()
  }
}
const validatePass =  (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}
const rules = reactive({
  username: [{ validator: validateName, trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }]
})

const goCompany = () => {
  ElMessage.warning('暂未开放，敬请期待！')
}

// 提交表单
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      login()
    } else {
      console.log('error submit!')
    }
  })
}
const encryptsm4 = (strsm4) => {
  let sm4Config = {
    // 配置sm4参数
    key: "48b31f058ac00df6", //这里这个key值是跟后端要的
    mode: "ecb", // 加密的方式有两种，ecb和cbc两种，也是看后端如何定义的，不过要是cbc的话下面还要加一个iv的参数，ecb不用
    cipherType: "base64",
  };
  return new sm4(sm4Config).encrypt(strsm4)
}
// 登录
const login = async () => {
  loading.value = true
  const params = {
    username: formData.username,
    password: encryptsm4(formData.password)
  }
  userStore
    .login(params)
    .then(async () => {
      await userStore.getUserInfo()
      // 需要在路由跳转前加载字典数据，否则会出现字典数据未加载完成导致页面渲染异常
      // await dictStore.loadDictionaries()

      // 跳转到登录前的页面
      const redirect = route.query.redirect || '/home'
      router.push(redirect)
    })
    .catch(() => {
      // getCaptcha() // 获取验证码
    })
    .finally(() => {
      loading.value = false
    })
}

</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  background: url('../../assets/images/login.png') no-repeat;
  background-size: cover;

  :deep(.el-form) {
    width: 100%;
    .el-button {
      width: 100%;
      margin-top: 10px;
    }
  }

  &_form {
    width: 25%;
    height: auto;
    padding: 25px;
    position: absolute;
    top: 50%;
    left: 60%;
    transform: translate(17%, -55%);
    background: #FFFFFF;
    border-radius: 6px;
  }
  &_title {
    font-size: 22px;
    font-family: Source Han Sans CN;
    font-weight: bold;
    color: #333333;
    margin-bottom: 20px;
  }
}
.bottom-info {
  position: absolute;
  bottom: 0;
  width: 100%;
  height: 48px;
  background: #12235c;
  text-align: center;
  line-height: 48px;
  span {
    font-size: 12px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    color: #3450b9;
    padding: 0 10px;
  }
}
</style>
